package com.program.facesigninsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.program.facesigninsystem.dao.IExamResultDao;
import com.program.facesigninsystem.dao.IUserInfoDao;
import com.program.facesigninsystem.entity.ResponseModel;
import com.program.facesigninsystem.entity.User;
import com.program.facesigninsystem.entity.UserInfo;
import com.program.facesigninsystem.iteration.entity.UserEntity;
import com.program.facesigninsystem.iteration.mapper.UserMapper;
import com.program.facesigninsystem.service.IUserInfoService;
import com.program.facesigninsystem.util.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static com.program.facesigninsystem.util.GetInfoUtil.getFileName;

@Service
public class UserInfoService implements IUserInfoService {
    private IUserInfoDao iUserInfoDao;
    private IExamResultDao iExamResultDao;
    private MailService iMailService;

    @Resource
    UserMapper userMapper;
    @Autowired
    public UserInfoService(IUserInfoDao iUserInfoDao,IExamResultDao iExamResultDao,MailService iMailService) {
        this.iUserInfoDao = iUserInfoDao;
        this.iExamResultDao = iExamResultDao;
        this.iMailService = iMailService;
    }

    @Override
    public UserInfo loginUser(String openid){
        return iUserInfoDao.loginUser(openid);
    }

    @Override
    public void updateToken(String uid,String token) {
        iUserInfoDao.updateToken(uid,token);
    }

    @Override
    public void addUser(String uid, String openid,String token,String userAvatar,String uid1,String alias) {
        iUserInfoDao.addUser(uid,openid,token,userAvatar,uid1,alias);
    }

    @Override
    public void updateUser(String uid, String name, String studentId, int type, String major, String uclass, String university, String academy,
                           String avatar,String sex,int age,String address,String phone,String email) {
        iUserInfoDao.updateUser(uid, name, studentId, type, major, uclass, university, academy,avatar,sex,age,address,phone,email);
    }

    @Override
    public Map<String,Object> selectUser(String uid) {
        return iUserInfoDao.selectUser(uid);
    }
    @Override
    public Object selectUserbyusername(String username, String password) {
        return iUserInfoDao.selectAdmin(username,password);
    }
    @Override
    public void addFace(String uid, byte[] facefeature,String createTime) {
        if(iUserInfoDao.selectFaceInfo(uid)!=null){
            iUserInfoDao.updateCreateTime(uid, facefeature, createTime);
        }else{
            iUserInfoDao.addFace(uid,facefeature,createTime);
        }
    }

    @Override
    public void updateAvatar(String uid, String avatar,String alias) {
        iUserInfoDao.updateAvatar(uid,avatar,alias);
    }

    @Override
    public byte[] selectFaceFeature(String uid) {
       return (byte[])iUserInfoDao.selectFaceFeature(uid);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Map<String,Object>> selectFaceFeatures(String uid) {
        Iterator iterator = iUserInfoDao.selectFaceFeatures(uid).iterator();
        Map<String,Object> map;
        List<Map<String,Object>> list = new ArrayList<>();
        while (iterator.hasNext()){
            map = (Map<String,Object>)iterator.next();
            list.add(map);
        }
        return list;
    }

    @Override
    public Object selectAdmin(String user, String password) {
        return iUserInfoDao.selectAdmin(user,password);
    }

    @Override
    public void adminToken(String user, String token) {
        iUserInfoDao.adminToken(user,token);
    }

    @Override
    public List<Map<String, Object>> selectList(String name,String university, String academy) {
        return iUserInfoDao.selectList(name,university, academy);
    }
    @Override
    public List<Map<String,Object>> selectListbygroupid(String name,String university, String academy,String uid){
        int groupid=iUserInfoDao.selectgroupid(uid);
        List<Integer> list1=iUserInfoDao.selectBygroupid(groupid);
        List<Integer> list=new ArrayList<>();
        list.addAll(list1);
        for(Integer l:list1){
            List<Integer> list2=iUserInfoDao.selectBygroupid(l);
            list.addAll(list2);
        }
        List<Map<String, Object>> mapList = new ArrayList<>();
        for(Integer l:list){
            List<Map<String, Object>> uList=iUserInfoDao.selectListbygroup(name,university, academy,l);
            mapList.addAll(uList);
        }
        return mapList;
    }
    @Override
    public void updateUserType(String uid, int type) {
        iUserInfoDao.updateUserType(uid,type);
    }

    @Override
    public Integer selectUsertypebyuid(String uid) {
        return iUserInfoDao.selectUserType(uid);
    }

    @Override
    public void updateIssued(String uid) {
        iUserInfoDao.updateIssued(uid);
    }

    @Override
    public void addTempList(String lid, String uid) {
        iUserInfoDao.addTempList(lid,uid);
    }

    @Override
    public List<Map<String, String>> selectTempList(String lid) {
        return iUserInfoDao.selectTempList(lid);
    }

    @Override
    public void deleteTempList(String lid) {
        iUserInfoDao.deleteTempList(lid);
    }

    @Override
    public String selectFaceInfo(String uid) {
        return iUserInfoDao.selectFaceInfo(uid);
    }

    @Override
    public UserInfo selectByUid1(String uid1) {
        return iUserInfoDao.selectByUid1(uid1);
    }

    @Override
    public void updatePassword(String uid1,String password,String salt) {
        iUserInfoDao.updatePassword(uid1,password,salt);
    }

    @Override
    public Map<String, Object> selectU(String uid1) {
        return iUserInfoDao.selectU(uid1);
    }

    @Override
    public void updateBindId(String uid, int bindId) {
        iUserInfoDao.updateBindId(uid, bindId);
    }

    @Override
    public void updateIsExamine(String uid, int isExamine) {
        iUserInfoDao.updateIsExamine(uid,isExamine);
    }

    @Override
    public void updateRoleId1(String uid, int roleId) {
        iUserInfoDao.updateRoleId1(uid, roleId);
    }

    @Override
    public List<UserInfo> selectUnExamine() {
        return iUserInfoDao.selectUnExamine();
    }

    @Override
    public void updateRoleId(int bindid, int roleid) {
        iUserInfoDao.updateRoleId(bindid, roleid);
    }

    @Override
    public UserInfo selectByBindid(int bindid) {
        return iUserInfoDao.selectByBindid(bindid);
    }

    @Override
    public Map<String, Object> selectExamine(String uid) {
        return iUserInfoDao.selectExamine(uid);
    }

    @Override
    public void insertResult(String uid, String result, String reason, String examTime) {
        iExamResultDao.insertResult(uid, result, reason, examTime);
    }

    @Override
    public List<Map<String, Object>> selectResultList(String uid) {
        return iExamResultDao.selectResultList(uid);
    }

    @Override
    public void updateIsRead(String uid, int id) {
        iExamResultDao.updateIsRead(uid, id);
    }

    @Override
    public ResponseModel selectCreateTime(String uid) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            String last = iUserInfoDao.selectCreateTime(uid);
            if(last == null){
                return new ResponseModel(200,"允许更新人脸");
            }
            long lastTime = simpleDateFormat.parse(last).getTime();
            long currentTime = System.currentTimeMillis();
            long diff = currentTime - lastTime;
            long expectedDiff = 1000*60*60*24*60L;
            if(diff<expectedDiff){
                long left = (expectedDiff-diff)/(1000*60*60*24L);
                return new ResponseModel(188,"距上次修改不足60天，请"+ left +"天后重试");
            }else{
                return new ResponseModel(200,"允许更新人脸");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new ResponseModel(500,"未知错误");
    }

    @Override
    public ResponseModel sendMail(String subject, String content, MultipartFile image) {
        try {
            String to = "a1750111263@qq.com";
//            File file = new File("C:\\images\\"+getFileName());
            File file = new File("\\root\\FaceSignIn\\images\\"+getFileName());
            if(!file.exists()){
                //noinspection ResultOfMethodCallIgnored
                file.createNewFile();
            }
            image.transferTo(file);
            content = "<html><body><div>"+content+"</div><img width='250px' src=\'cid:img110\'></body></html>";
            iMailService.sendInlineResourceMail(to, subject, content, file, "img110");
        }catch (Exception e) {
            e.printStackTrace();
            return new ResponseModel(199,"发送失败");
        }
        return new ResponseModel(200,"发送成功");
    }

    @Override
    public Integer getRoleId(String uid) {
        UserEntity userEntity = userMapper.selectOne(new QueryWrapper<UserEntity>().eq("uid", uid));
        if (userEntity.getRoleid() == null){
            userEntity.setRoleid(0);
            userMapper.update(userEntity, new QueryWrapper<UserEntity>().eq("uid", uid));
            return 0;
        }
        return userEntity.getRoleid();
    }
}
